<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
		"http://www.w3.org/TR/html4/strict.dtd">
<html>
	<head>
		<title>doh.robot Button Test</title>

		<style>
			@import "../../../../util/doh/robot/robot.css";
		</style>

		<!-- required: dojo.js -->
		<script type="text/javascript" src="../../../../dojo/dojo.js"
			djConfig="isDebug: true, parseOnLoad: true"></script>

		<script type="text/javascript" src="../../helpers.js"></script>

		<script type="text/javascript">
			dojo.require("dijit.dijit"); // optimize: load dijit layer
			dojo.require("dijit.robotx");

			dojo.addOnLoad(function(){
				doh.robot.initRobot('../test_Button.html');

				doh.register("dijit.form.Button", [
					{
						name: "enabled",
						timeout: 15000,
						runTest: function(){
							var d = new doh.Deferred();

							doh.robot.sequence(function(){ dojo.byId("input").focus(); });

							var clicks = 0;
							dijit.byId("1465").attr("onClick", function(){ clicks++; });


							// tab into button to get focus	
							doh.robot.keyPress(dojo.keys.TAB, 100, {});

							// and activate	with space / enter
							doh.robot.keyPress(dojo.keys.SPACE, 100, {});
							doh.robot.keyPress(dojo.keys.ENTER, 100, {});

							doh.robot.sequence(d.getTestCallback(function(){
								doh.is(2, clicks, "both space and enter click the button");
							}), 100);
							return d;
						}
					},
					{
						name: "disabled no focus",
						timeout: 15000,
						runTest: function(){
							var d = new doh.Deferred();

							dijit.byId("1467").attr("disabled", true);

							// tab over disabled "View" button to "Create" combo-button	
							doh.robot.keyPress(dojo.keys.TAB, 100, {});

							doh.robot.sequence(d.getTestCallback(function(){
								console.log("current focus is ", dojo.global.dijit._curFocus);
								doh.is("comboCreate", dijit.getEnclosingWidget(dojo.global.dijit._curFocus).id, "focus jumped to combo create (left part of button)");
							}), 100);
							return d;
						}
					}
				]);

				doh.register("dijit.form.DropDownButton", [
					{
						name: "down arrow opens menu",
						timeout: 15000,
						runTest: function(){
							var d = new doh.Deferred();
	
							dijit.byId("edit").focus();
							doh.robot.keyPress(dojo.keys.DOWN_ARROW, 100, {});
	
							doh.robot.sequence(d.getTestCallback(function(){
								doh.t(isVisible("editMenu"), "edit menu is visible: " + dijit.byId("editMenu").domNode.style.cssText);
								doh.is("cut", dojo.global.dijit._curFocus.id, "focus is on menu");
							}), 1000);
							return d;
						}
					},
					
					{
						name: "esc closes menu",
						timeout: 15000,
						runTest: function(){
							var d = new doh.Deferred();
	
							doh.robot.keyPress(dojo.keys.ESCAPE, 100, {});
	
							doh.robot.sequence(d.getTestCallback(function(){
								doh.t(isHidden("editMenu"), "edit menu is hidden: " + dijit.byId("editMenu").domNode.style.cssText);
								doh.is("edit", dojo.global.dijit._curFocus.id, "focus is back on button");
							}), 1000);
							return d;
						}
					},
					{
						name: "space opens menu",
						timeout: 15000,
						runTest: function(){
							var d = new doh.Deferred();
	
							dijit.byId("edit").focus();
							doh.robot.keyPress(dojo.keys.SPACE, 100, {});
	
							doh.robot.sequence(d.getTestCallback(function(){
								doh.t(isVisible("editMenu"), "edit menu is visible: " + dijit.byId("editMenu").domNode.style.cssText);
							}), 1000);
							return d;
						}
					},
					
					{
						name: "space executes menu",
						timeout: 15000,
						runTest: function(){
							var d = new doh.Deferred();
	
							doh.robot.keyPress(dojo.keys.SPACE, 100, {});
	
							doh.robot.sequence(d.getTestCallback(function(){
								doh.t(isHidden("editMenu"), "edit menu is hidden: " + dijit.byId("editMenu").domNode.style.cssText);
								doh.is("edit", dojo.global.dijit._curFocus.id, "focus is back on button");
							}), 1000);
							return d;
						}
					},
					{
						name: "enter opens menu",
						timeout: 15000,
						runTest: function(){
							var d = new doh.Deferred();
	
							dijit.byId("edit").focus();
							doh.robot.keyPress(dojo.keys.ENTER, 100, {});
	
							doh.robot.sequence(d.getTestCallback(function(){
								doh.t(isVisible("editMenu"), "edit menu is visible: " + dijit.byId("editMenu").domNode.style.cssText);
							}), 1000);
							return d;
						}
					},
					
					{
						name: "tab key closes popup, goes back to button",
						timeout: 15000,
						runTest: function(){
							var d = new doh.Deferred();
	
							doh.robot.keyPress(dojo.keys.TAB, 100, {});
	
							doh.robot.sequence(d.getTestCallback(function(){
								doh.t(isHidden("editMenu"), "edit menu is hidden: " + dijit.byId("editMenu").domNode.style.cssText);
								doh.is("edit", dojo.global.dijit._curFocus.id, "focus is back on button");
							}), 1000);
							return d;
						}
					}

				]);

				doh.register("dijit.form.ComboButton", [
					{
						name: "tab to button",
						timeout: 15000,
						runTest: function(){
							var d = new doh.Deferred();

							doh.robot.sequence(function(){
								dijit.byId("color").focus();
							});

							doh.robot.keyPress(dojo.keys.TAB, 100, {});
	
							var leftPart = dojo.query(".dijitButtonContents", dojo.byId("save"))[0];
							doh.t(leftPart, "found left part of button");

							doh.robot.sequence(d.getTestCallback(function(){
								doh.is(leftPart, dojo.global.dijit._curFocus, "focused on left part");
								console.log("cur focus: ",  dojo.global.dijit._curFocus);
							}), 100);
							return d;
						}
					},
	
					{
						name: "click button",
						timeout: 15000,
						runTest: function(){
							var d = new doh.Deferred();

							var clicked = false;
							dijit.byId("save").attr("onClick", function(){ clicked = true; });
	
							doh.robot.keyPress(dojo.keys.ENTER, 100, {});

							doh.robot.sequence(d.getTestCallback(function(){
								doh.t(clicked, "button was clicked");
							}), 100);
							return d;
						}
					},

					{
						name: "tab to drop down arrow",
						timeout: 15000,
						runTest: function(){
							var d = new doh.Deferred();
	
							var rightPart = dojo.query(".dijitDownArrowButton", dojo.byId("save"))[0];
							doh.t(rightPart, "found right part of button");

							doh.robot.keyPress(dojo.keys.TAB, 100, {});
	
							doh.robot.sequence(d.getTestCallback(function(){
								doh.is(rightPart, dojo.global.dijit._curFocus, "focused on right part of combo");
							}), 500);
							return d;
						}
					},

					{
						name: "down arrow opens menu",
						timeout: 15000,
						runTest: function(){
							var d = new doh.Deferred();
	
							var rightPart = dojo.query(".dijitDownArrowButton", dojo.byId("save"))[0];
							doh.t(rightPart, "found right part of button");

							doh.robot.keyPress(dojo.keys.DOWN_ARROW, 100, {});
	
							doh.robot.sequence(d.getTestCallback(function(){
								doh.t(isVisible("saveMenu"), "save menu is visible: " + dijit.byId("saveMenu").domNode.style.cssText);
							}), 500);
							return d;
						}
					},
				
					{
						name: "esc closes menu",
						timeout: 15000,
						runTest: function(){
							var d = new doh.Deferred();
	
							var rightPart = dojo.query(".dijitDownArrowButton", dojo.byId("save"))[0];
							doh.t(rightPart, "found right part of button");

							doh.robot.keyPress(dojo.keys.ESCAPE, 100, {});
	
							doh.robot.sequence(d.getTestCallback(function(){
								doh.t(isHidden("saveMenu"), "save menu was closed: " + dijit.byId("saveMenu").domNode.style.cssText);
								doh.is(rightPart, dojo.global.dijit._curFocus, "refocused on right part of combo");
							}), 500);
							return d;
						}
					},

					{
						name: "enter key opens menu",
						timeout: 15000,
						runTest: function(){
							var d = new doh.Deferred();
	
							doh.robot.keyPress(dojo.keys.ENTER, 100, {});
	
							doh.robot.sequence(d.getTestCallback(function(){
								doh.t(isVisible("saveMenu"), "save menu is visible: " + dijit.byId("saveMenu").domNode.style.cssText);
							}), 500);
							return d;
						}
					},
					
					{
						name: "tab closes menu",
						timeout: 15000,
						runTest: function(){
							var d = new doh.Deferred();
	
							var rightPart = dojo.query(".dijitDownArrowButton", dojo.byId("save"))[0];
							doh.t(rightPart, "found right part of button");

							doh.robot.keyPress(dojo.keys.TAB, 100, {});
	
							doh.robot.sequence(d.getTestCallback(function(){
								doh.t(isHidden("saveMenu"), "save menu was closed: " + dijit.byId("saveMenu").domNode.style.cssText);
								doh.is(rightPart, dojo.global.dijit._curFocus, "refocused on right part of combo");
							}), 500);
							return d;
						}
					}
	
/*** 
	commented out until #8903 is fixed				
					,

					{
						name: "tab closes menu",
						timeout: 15000,
						runTest: function(){
							var d = new doh.Deferred();

							doh.robot.sequence(function(){
								// Focus element before "save" ComboButton
								dijit.byId("color").focus();
								
								// And disable the ComboBox.
								dijit.byId("save").attr("disabled", true);
							});

							// Tab should skip over the ComboButton entirely
							doh.robot.keyPress(dojo.keys.TAB, 100, {});
	
							doh.robot.sequence(d.getTestCallback(function(){
								var focusedWidget = dijit.getEnclosingWidget(dojo.global.dijit._curFocus);
								doh.isNot("save", focusedWidget && focusedWidget.id, "didn't focus on combo");
							}), 500);
							return d;
						}
					}
***/
				]);

				doh.register("dijit.form.ToggleButton", [
					{
						name: "change value",
						timeout: 15000,
						runTest: function(){
							var d = new doh.Deferred();
	
							var checked = dijit.byId("toggle1").attr("checked");
							dijit.byId("toggle1").attr("onChange", function(v){ checked = v; });
							doh.t(checked, "toggle1 initially checked");

							doh.robot.sequence(function(){
								dijit.byId("toggle1").focus();
							});
							doh.robot.keyPress(dojo.keys.SPACE, 100, {});
	
							doh.robot.sequence(d.getTestCallback(function(){
								doh.f(checked, "toggle1 unchecked");
							}), 100);
							return d;
						}
					}
				]);

				doh.run();
			});
		</script>
	</head>
</html>
